{php include wl_template('common/header');}
<ul class="nav nav-tabs" id="myTab">
	<li class="active"><a href="#">添加商家</a></li>
</ul>
<div class="app-content">
	<div class="app-form">
		<form action="" method="post" class="form-horizontal form form-validate" id="commentForm">
			<div class="panel panel-default">
				<div class="panel-heading">活动信息</div>
				<div class="panel-body">
					{if !is_store()}
					<div class="form-group">
						<label class="col-sm-2 control-label">选择商户</label>
						<div class="col-sm-9">
							<div class='input-group'>
								<div class="input-group ">
									<input type="text" class="form-control col-sm-9" id="namemerchant" name="" value="{$halfcard['storename']}" style="width: 460px;" disabled>
									<span class="input-group-btn">
								<button class="btn btn-default" type="button" onclick="popwin = $('#modal-module-merchant').modal();">选择商家</button></span>
								</div>
								<div class="input-group " style="margin-top:.5em;">
									<input type="hidden" value="{$halfcard['merchantid']}" name="halfcard[merchantid]" id="sidmerchant" >
									<img src="{if empty($halfcard['logo'])}../web/resource/images/nopic.jpg{else}{php echo tomedia($halfcard['logo'])}{/if}" class="img-responsive img-thumbnail" width="150" id="imgmerchant" />
									<em class="close" style="position:absolute; top: 0px; right: -14px;" title="删除" onclick="remove_merchant(this)">×</em>
								</div>
							</div>
						</div>
					</div>
					{/if}
					<div class="form-group"  style="display: block;">
						<label class="col-sm-2 control-label">活动名称</label>
						<div class="col-sm-9">
							<input type="text" name="halfcard[title]" class="form-control" value="{$halfcard['title']}" id="storetitle" />
							<span class="help-block">填写活动名称，默认与商户名称相同</span>
						</div>
					</div>
					<!--一卡通-->
					<div class="form-group">
						<label class="col-sm-2 control-label">折扣方式</label>
						<div class="col-sm-9">
							<label class="radio-inline" onclick="$('.les1').show();$('.les2').hide();$('#levellimit').show();">
								<input type="radio" value="0" name="halfcard[levelstatus]" {if $halfcard['levelstatus'] == 0 || empty($halfcard) } checked {/if}>通用折扣
							</label>
							<label class="radio-inline" onclick="$('.les1').hide();$('.les2').show();$('#levellimit').hide();">
								<input type="radio" value="1" name="halfcard[levelstatus]" {if $halfcard['levelstatus']==1} checked {/if}>分级折扣
							</label>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">特权折扣时间类型</label>
						<div class="col-sm-9">
							<label class="radio-inline">
								<input type="radio" value="1" id="wk" name="datestatus" {if $halfcard['datestatus'] == 1 || empty($halfcard) } checked {/if}>按星期
							</label>
							<label class="radio-inline">
								<input type="radio" value="2" id="dd" name="datestatus" {if $halfcard['datestatus']==2} checked {/if}>按日期
							</label>
							<label class="radio-inline">
								<input type="radio" value="3" id="cc" name="datestatus" {if $halfcard['datestatus']==3} checked {/if}>关闭
							</label>
						</div>
					</div>
					<script>
						$('#wk').click(function(){
							$('#weeke').show();
							$('#activediscount').show();
							$('#daily').hide();
						});
						$('#dd').click(function(){
							$('#weeke').hide();
							$('#activediscount').show();
							$('#daily').show();
						});
						$('#cc').click(function(){
							$('#weeke').hide();
							$('#activediscount').hide();
							$('#daily').hide();
						});
					</script>
					<div class="form-group" {if $halfcard['datestatus'] == 2 || $halfcard['datestatus'] == 3} style="display: none;"{/if} id="weeke">
						<label class="col-sm-2 control-label">按星期</label>
						<div class="col-sm-9">
							{if $halfcard}
							<label class="checkbox-inline">
								<input type="checkbox" value="1" name="halfcard[week][]" id="wk1"  /> 星期一
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="2" name="halfcard[week][]" id="wk2"  /> 星期二
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="3" name="halfcard[week][]" id="wk3"  /> 星期三
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="4" name="halfcard[week][]" id="wk4"  /> 星期四
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="5" name="halfcard[week][]" id="wk5"  /> 星期五
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="6" name="halfcard[week][]" id="wk6"  /> 星期六
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="7" name="halfcard[week][]" id="wk7"  /> 星期日
							</label>
							{loop $halfcard['week'] $week}
							<script type="text/javascript">
								if({php echo $week}==1){
									$('#wk1').attr('checked','true');
								}
								if({php echo $week}==2){
									$('#wk2').attr('checked','true');
								}
								if({php echo $week}==3){
									$('#wk3').attr('checked','true');
								}
								if({php echo $week}==4){
									$('#wk4').attr('checked','true');
								}
								if({php echo $week}==5){
									$('#wk5').attr('checked','true');
								}
								if({php echo $week}==6){
									$('#wk6').attr('checked','true');
								}
								if({php echo $week}==7){
									$('#wk7').attr('checked','true');
								}

							</script>
							{/loop}
							{else}
							<label class="checkbox-inline">
								<input type="checkbox" value="1" name="halfcard[week][]" /> 星期一
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="2" name="halfcard[week][]" /> 星期二
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="3" name="halfcard[week][]" /> 星期三
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="4" name="halfcard[week][]" /> 星期四
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="5" name="halfcard[week][]" /> 星期五
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="6" name="halfcard[week][]" /> 星期六
							</label>
							<label class="checkbox-inline">
								<input type="checkbox" value="7" name="halfcard[week][]" /> 星期日
							</label>
							{/if}
						</div>
					</div>
					<div class="form-group" {if $halfcard['datestatus'] == 1 || empty($halfcard) || $halfcard['datestatus'] == 3 } style="display: none;"{/if} id="daily">
						<label class="col-sm-2 control-label">按天数</label>
						<div class="col-sm-9">
							{if $halfcard['datestatus'] == 2}
							<?php
								for ($i=1;$i<32;$i++ ) {
									if(in_array($i,$halfcard['day'])){
										?>
							<label class="checkbox-inline">
								<input type="checkbox" value="{php echo $i}"  checked name="halfcard[day][]" />{php echo $i}
							</label>
							<?php
								}else{
								?>
							<label class="checkbox-inline">
								<input type="checkbox" value="{php echo $i}"  name="halfcard[day][]" />{php echo $i}
							</label>
							<?php
								}}
							  ?>
							{else}
							<?php
							for ($i=1;$i<32;$i++ ) {
						  ?>
							<label class="checkbox-inline">
								<input type="checkbox" value="{php echo $i}"  name="halfcard[day][]" />{php echo $i}
							</label>
							<?php
							}
						  ?>
							{/if}
						</div>
					</div>
					<div {if $halfcard['datestatus'] == 3} style="display: none;" {/if} id="activediscount">
						<div class="form-group les1" {if $halfcard['levelstatus'] == 1} style="display:none;"  {/if} >
							<label class="col-sm-2 control-label">活动折扣</label>
							<div class="col-sm-9">
								<div class="input-group">
									<input type="number" min="0.1" max="10" class="form-control" name="halfcard[activediscount]" value="{$halfcard[activediscount]}"/>
									<span class="input-group-addon">折</span>
								</div>
								<span class="help-block">请输入数字,保留一位小数</span>
							</div>
						</div>
						<div class="form-group les2" {if $halfcard['levelstatus'] != 1} style="display:none;"  {/if}>
							<label class="col-sm-2 control-label">活动折扣</label>
							<div class="col-sm-9">
								<div style="margin-top: 10px;">
									<div class="input-group data-item">
										<span class="input-group-addon">[{$_W['wlsetting']['halflevel']['name']}]</span>
										<input type="hidden" name="le_ac_id[]" class="form-control" value="0">
										<input type="number" min="0.1" max="10" name="le_ac_pr[]" class="form-control" value="{$le_ac_array[0]}">
										<span class="input-group-addon">折</span>
									</div>
								</div>
								{loop $levels $level}
								<div style="margin-top: 10px;">
									<div class="input-group data-item">
										<span class="input-group-addon">[{$level['name']}]</span>
										<input type="hidden" name="le_ac_id[]" class="form-control" value="{$level['id']}">
										<input type="number" min="0.1" max="10" name="le_ac_pr[]" class="form-control" value="{$le_ac_array[$level['id']]}">
										<span class="input-group-addon">折</span>
									</div>
								</div>
								{/loop}
								<span class="help-block">请输入数字,保留一位小数</span>
							</div>
						</div>
					</div>
					<div class="form-group"  style="display: block;">
						<label class="col-sm-2 control-label">平日折扣</label>
						<div class="col-sm-9">
							<input type="checkbox" class="js-switch" name="halfcard[daily]" onclick="asd()" {if $halfcard['daily'] == 1} checked="checked" {/if}>
							<input type="hidden" id="dailyflag" {if $halfcard['daily'] == 1 } value="1" {else} value="0" {/if} />
						</div>
					</div>
					<div id="discount" style="display: block;" >
						<div class="form-group les1" {if $halfcard['levelstatus'] == 1} style="display:none;"  {/if}>
							<label class="col-sm-2 control-label">折扣额度</label>
							<div class="col-sm-9">
								<div class="input-group">
									<input type="number" min="0.1" max="10" class="form-control" name="halfcard[discount]" value="{$halfcard[discount]}"/>
									<span class="input-group-addon">折</span>
								</div>
								<span class="help-block">请输入数字,保留一位小数</span>
							</div>
						</div>
						<div class="form-group les2" {if $halfcard['levelstatus'] != 1} style="display:none;"  {/if}>
							<label class="col-sm-2 control-label">折扣额度</label>
							<div class="col-sm-9">
								<div style="margin-top: 10px;">
									<div class="input-group data-item">
										<span class="input-group-addon">[{$_W['wlsetting']['halflevel']['name']}]</span>
										<input type="hidden" name="le_day_id[]" class="form-control" value="0">
										<input type="number" min="0.1" max="10" name="le_day_pr[]" class="form-control" value="{$le_day_array[0]}">
										<span class="input-group-addon">折</span>
									</div>
								</div>
								{loop $levels $level}
								<div style="margin-top: 10px;">
									<div class="input-group data-item">
										<span class="input-group-addon">[{$level['name']}]</span>
										<input type="hidden" name="le_day_id[]" class="form-control" value="{$level['id']}">
										<input type="number" min="0.1" max="10" name="le_day_pr[]" class="form-control" value="{$le_day_array[$level['id']]}">
										<span class="input-group-addon">折</span>
									</div>
								</div>
								{/loop}
								<span class="help-block">请输入数字,保留一位小数</span>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">使用限制</label>
						<div class="col-sm-9">
							<input type="text" name="halfcard[limit]" class="form-control" value="{$halfcard['limit']}" id="use_limit" />
							<span class="help-block">如：300元以内；酒水饮料除外；两人同行一人免单</span>
						</div>
					</div>
					<div class="form-group" id="levellimit" {if $halfcard['levelstatus'] == 1} style="display:none;" {/if}>
						<label class="col-sm-2 control-label">等级限制</label>
						<div class="col-sm-9">
							<label class="checkbox-inline">
								<input type="checkbox" value="0" {if in_array(0,$halfcard['level'])} checked {/if}  name="halfcard[level][]" />{$_W['wlsetting']['halflevel']['name']}
							</label>
							{loop $levels $level}
							<label class="checkbox-inline">
								<input type="checkbox" value="{$level['id']}" {if in_array($level['id'],$halfcard['level'])} checked {/if}  name="halfcard[level][]" />{$level['name']}
							</label>
							{/loop}
							<span class="help-block">如果全都不勾选则默认全等级可用</span>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">次数限制</label>
						<div class="col-sm-9">
							<input type="tel" name="halfcard[timeslimit]" class="form-control" value="{$halfcard['timeslimit']}" id="time_limit" />
							<span class="help-block">商家每天提供折扣的次数，填0或不填则无限制</span>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label">使用说明</label>
						<div class="col-sm-9">
							{php echo tpl_diy_editor_create('halfcard[describe]', $halfcard['describe']);}
						</div>
					</div>
					{if !is_store()}
					<div class="form-group">
						<label class="col-sm-2 control-label">排序</label>
						<div class="col-sm-9">
							<input type="tel" class="form-control" name="halfcard[sort]" value="{$halfcard[sort]}"/>
							<span class="help-block">请输入整数数字，序号越大，排序靠前</span>
						</div>
					</div>
					{/if}
					<div class="form-group">
						<label class="col-sm-2 control-label">状态</label>
						<div class="col-sm-9">
							<input type="checkbox" class="js-switch" name="halfcard[status]" {if $halfcard['status'] == 1 || empty($halfcard) } checked="checked" {/if}>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-2 control-label"></label>
						<div class="col-sm-9">
							<input type="submit" name="submit" value="提交" class="btn btn-primary min-width" />
							<input type="hidden" name="token" value="{$_W['token']}" />
						</div>
					</div>
				</div>
			</div>
			<div id="modal-module-merchant" class="modal fade" tabindex="-1">
				<div class="modal-dialog" style='width: 920px;'>
					<div class="modal-content">
						<div class="modal-header">
							<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
							<h3>选取</h3></div>
						<div class="modal-body">
							<div class="row">
								<div class="input-group">
									<input type="text" class="form-control" name="keyword" value="" id="search-kwd-merchant" placeholder="请输入商家名称，不输入任何内容搜索结果为所有商家。" />
									<span class='input-group-btn'><button type="button" class="btn btn-default" onclick="search_merchant();">搜索</button></span>
								</div>
							</div>
							<div id="module-merchant" style="padding-top:5px;"></div>
						</div>
						<div class="modal-footer"><a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</a></div>
					</div>
				</div>
			</div>
			</div>
		</form>
	</div>
</div>
<script>
$(function () {
	window.optionchanged = false;
	$('#myTab a').click(function (e) {
		e.preventDefault();//阻止a链接的跳转行为
		$(this).tab('show');//显示当前选中的链接及关联的content
	});
	var dailyflag = $('#dailyflag').val();
	//alert(dailyflag);
	if(dailyflag == 0){
		$('#discount').hide();
	}else{
		$('#discount').show();
	}
});
</script>
<script language='javascript'>
	$('#commentForm').submit(function(){
		if($('#sidmerchant').val() == '') {
	    	util.tips("请选择商户");
			return false;
		}
		if($('#storetitle').val() == '') {
	    	util.tips("请填写活动名称");
			return false;
		}
		var timetype = $(':radio[name="datestatus"]:checked').val();
		if (timetype == 1) {
			var time = $(':checkbox[name="halfcard[week][]"]:checked').val();
			if(!time) {
		    	util.tips("请选择每周特权活动时间");

				return false;
			}
		}else if(timetype == 2){
			var time = $(':checkbox[name="halfcard[day][]"]:checked').val();
			if(!time) {
		    	util.tips("请选择每月特权活动时间");
				return false;
			}
		}
		if(!$.trim($(':input[name="halfcard[limit]"]').val())){
			util.tips("请填写使用限制，无限制请填无");
			return false;
		}
		if ($('#dailyflag').val()){
			var x = $.trim($(':input[name="halfcard[discount]"]').val());
			if (isNaN(x)){
				util.tips("请填写规范的平日折扣额度");
				return false;
			}
		}
//		if(!$.trim($(':input[name="halfcard[adv]"]').val())){
//			util.tips("请至少选择一张幻灯片");
//			return false;
//		}
// 		if(!$.trim($(':input[name="halfcard[describe]"]').val())){
// 			util.tips("请填写使用说明");
// 			return false;
// 		}
// 		if ($('#inspectflag').val() == 2){
// 			util.tips("所选商户已有特权活动");
// 			return false;
// 		}

		return true;
	});

	function inspect(){
		var merchantid = $('#sidmerchant').val();
		$.post("{php echo web_url('halfcard/halfcard_web/inspect')}", { id : merchantid,type:1 }, function(data){
			if(data.errno){
				util.tips("该商户已有特权活动");
			}else{
				if($('#storetitle').val().length < 1){
					$('#storetitle').val($('#namemerchant').val());
				}
				util.tips("操作成功");
			}
		}, 'json');
	}

	function search_merchant() {
			$("#module-merchant").html("正在搜索....")
			$.get("{php echo web_url('halfcard/halfcard_web/selectMerchant',array('type'=>1))}", {
				keyword: $.trim($('#search-kwd-merchant').val())
			}, function(dat){
				$('#module-merchant').html(dat);
			});
		}
	function remove_merchant(obj){
        $('#goodsidmerchant').val('');
        $('#namemerchant').val('');
        $('#imgmerchant').attr("src",'');
        $('#sidmerchant').val('');
       }
	function select_merchant(o) {
		var lastid = $('#sidmerchant').val();
		if (lastid != o.id) {
			$('#sidmerchant').val(o.id);
        	$('#namemerchant').val(o.storename);
        	$('#imgmerchant').attr("src",o.logo);
	    	$('#modal-module-merchant').modal('hide');
			inspect();
		}else{
	    	$('#modal-module-merchant').modal('hide');
		}
 	}
	function asd(){
		var dailyflag = $('#dailyflag').val();
		if (dailyflag == 0) {
			$('#discount').show();
			$('#dailyflag').val(1);
		}else{
			$('#discount').hide();
			$('#dailyflag').val(0);
		}
	}

</script>
{php include wl_template('common/footer');}